<?php
/**
 * 后台-推荐位管理
 * PositionController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author: huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class PositionController extends AdminController
{   
    protected $oModel;
    protected $_attributeLabels;
	public function __construct($id, $module) {
	    //模型
		$this->oModel = new BasePositionModel();
        $this->_attributeLabels = $this->oModel->attributeLabels();
		return parent::__construct($id, $module);
	}
	public function actionIndex(){
	    $aParams['pagesize'] = $this->pagesize;
		$aParams['page'] = $this->_request->getQuery('page' ,0);
		$aResult = $this->oModel->getList($aParams);
        $aResult['params'] = $aParams;	
		$this->render('index', $aResult);
	}
    /**
     * PositionController::actionAdd()
     * 
     * @return void
     */
    public function actionAdd(){
        $aPostData = $this->_request->getPost('BasePositionModel');
        if( $aPostData ){
            $this->oModel->attributes = $aPostData;
            $this->oModel->save();
            $alert="SendWebMsg('success','添加推荐位成功','".$this->createUrl('position/index')."')";
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
        $this->render('_form');
    }
    /**
     * PositionController::actionEdit()
     * 
     * @return void
     */
    public function actionEdit(){
        $iPosid = $this->_request->getQuery('posid');
        $this->oModel = $this->oModel->findByPk($iPosid);
        $aPostData = $this->_request->getPost('BasePositionModel');
        if( $aPostData ){
            $this->oModel->attributes = $aPostData;
            $this->oModel->save();
            $alert="SendWebMsg('success','编辑成功','".$this->createUrl('position/index')."')";
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
        $this->render('_form');
    }
    /**
     * PositionController::actionAjaxDisable()
     * 
     * @return void
     */
    public function actionAjaxDisable(){
        $aResulte = array();
        $posid = $this->_request->getQuery('posid');
        $this->oModel = $this->oModel->findByPk($posid);
        $aData = $this->oModel->attributes;
        if( ! empty( $aData ) ){
            $aData['disable'] = $aData['disable']==1 ? 2 : 1;
            $sDisable = $aData['disable']==1 ? '禁用' : '启用';
            $sTextRed = $aData['disable']==1 ? '' : 'red';
            $mResult = $this->oModel->saveData($aData);
            $html = "<a href='javascript:void(0)' class='ajaxOperateRplace $sTextRed' url='".$this->createUrl('position/ajaxDisable' ,array('posid'=>$posid))."'>". $sDisable ."</a>";
            if( is_string($mResult) ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '状态更新成功',
                    'data' => array('html'=>$html)
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '更新状态失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '信息错误，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
    /**
     * PositionController::actionAjaxChangeModel() 获取模型下面的栏目
     * 
     * @return json 模型下面选项值下拉
     */
    public function actionAjaxChangeModel(){
        $params = array();
        $iModelId = $this->_request->getQuery('model_id');
        if( $iModelId ) $params['model_id'] = $iModelId;
        $sHtml = '<select name="BasePositionModel[cate_id]" class="input-normal">
                    <option value="0">顶级栏目</option>';
        $fid = isset($this->oModel->parent_id) ? (int)$this->oModel->parent_id : 0;
        $menuData = AdminCategory::getCategoryData($params);
        $tree = new Tree($menuData);
        $tree->icon = array('&nbsp;│ ','&nbsp;├─ ','&nbsp;└─ ');
        $tree->nbsp = '&nbsp;';
        $str="<option value='\$id' \$selected>\$spacer\$cate_name</option>";
        $categorys = $tree->get_tree(0, $str, $fid);
        $sHtml .= $categorys;            
        $sHtml .= '</select>';
        $aResult = array(
                        'err' => 0,
                        'msg' => 'success',
                        'data' => $sHtml
                    );
        die(json_encode($aResult));
    }
    /**
     * ModuleController::actionajaxSort() 排序
     * 
     * @return void
     */
    public function actionAjaxSort(){
        $aResulte = array();
        $posid = $this->_request->getQuery('posid');
        $iSort = $this->_request->getQuery('sort');
        $this->oModel = $this->oModel->findByPk($posid);
        $aData = $this->oModel->attributes;
        if( ! empty( $aData ) ){
            $aData['sort'] = $iSort;
            $mResult = $this->oModel->saveData($aData ,false);
            if( !is_array($mResult) ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '排序成功'
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '排序失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '排序失败，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
    /**
     * PositionController::actionDelete() 删除
     * 
     * @return json 操作结果JSON数据
     */
    public function actionDelete(){
        $aResult = array();
        $postid = $this->_request->getQuery('posid' ,0);
        $mResult = $this->oModel->deleteData($postid);
        if( $mResult ){
            $aResulte = array(
                'err' => 0 ,
                'msg' => '删除成功'
            );
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '删除失败'
            );
        }
        die(json_encode($aResulte));
    }
}
